package demo;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.print.PrinterException;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;

public class RightMenuDemo extends JPopupMenu implements MouseListener,
		ActionListener {
	private static RightMenuDemo sharedInstance = null;

	public static void installToComponent(JTextComponent c) {
		if (c instanceof JTextField && !(c instanceof JPasswordField)) {
			c.addMouseListener(RightMenuDemo.getSharedInstance());
		}
	}

	public static void uninstallFromComponent(JTextComponent c) {
		if (c instanceof JTextField && !(c instanceof JPasswordField)) {
			c.removeMouseListener(getSharedInstance());
		}
	}

	JMenuItem cutItem, copyItem, pasteItem, deleteItem, selectAllItem,
			printItem;

	public RightMenuDemo() {
		add(cutItem = new JMenuItem("剪切   (T)"));
		add(copyItem = new JMenuItem("复制   (C)"));
		add(pasteItem = new JMenuItem("粘贴   (P)"));
		add(deleteItem = new JMenuItem("删除   (D)"));
		addSeparator();
		add(selectAllItem = new JMenuItem("全选   (A)"));
		addSeparator();
		add(printItem = new JMenuItem("打印   (R)"));

		cutItem.setMnemonic('T');
		copyItem.setMnemonic('C');
		pasteItem.setMnemonic('P');
		deleteItem.setMnemonic('D');
		selectAllItem.setMnemonic('A');
		printItem.setMnemonic('R');

		cutItem.addActionListener(this);
		copyItem.addActionListener(this);
		pasteItem.addActionListener(this);
		deleteItem.addActionListener(this);
		selectAllItem.addActionListener(this);
		printItem.addActionListener(this);
	}

	private static RightMenuDemo getSharedInstance() { // 单例模式
		if (sharedInstance == null) {
			sharedInstance = new RightMenuDemo();
		}
		return sharedInstance;
	}

	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger() && e.getSource() instanceof JTextField) { // e.isPopupTrigger()鼠标右键
			JTextField textfield = (JTextField) e.getSource();
			if (Boolean.TRUE.equals(textfield
					.getClientProperty("DisablePopupMenu"))) {
				return;
			}
			textfield.requestFocusInWindow();
			this.show(textfield, e.getX(), e.getY()); // 获得组件及位置
		}
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void show(Component invoker, int x, int y) {
		JTextComponent tc = (JTextComponent) invoker;
		String sel = tc.getSelectedText();

		boolean selected = sel != null && !sel.equals("");
		boolean enableAndEditable = tc.isEnabled() && tc.isEditable();
		// 禁用与启用
		cutItem.setEnabled(selected && enableAndEditable);
		copyItem.setEnabled(selected && tc.isEnabled());
		deleteItem.setEnabled(selected && enableAndEditable);
		pasteItem.setEnabled(enableAndEditable);
		selectAllItem.setEnabled(tc.isEnabled());
		super.show(invoker, x, y);
	}

	public void actionPerformed(ActionEvent e) {
		JTextComponent tc = (JTextComponent) getInvoker();
		String sel = tc.getSelectedText();

		if (e.getSource() == cutItem) {
			tc.cut();
		} else if (e.getSource() == copyItem) {
			tc.copy();
		} else if (e.getSource() == pasteItem) {
			tc.paste();
		} else if (e.getSource() == selectAllItem) {
			tc.selectAll();
		} else if (e.getSource() == deleteItem) {
			Document doc = tc.getDocument();
			int start = tc.getSelectionStart();
			int end = tc.getSelectionEnd();

			try {
				Position p0 = doc.createPosition(start);
				Position p1 = doc.createPosition(end);

				if ((p0 != null) && (p1 != null)
						&& (p0.getOffset() != p1.getOffset())) {
					doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
				}
			} catch (BadLocationException be) {
			}
		}
		if (e.getSource() == printItem) {
			try {
				tc.print();
			} catch (PrinterException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager
					.getCrossPlatformLookAndFeelClassName()); // 适合所有平台的观感
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JTextField tf = new JTextField(10);
		tf.addMouseListener(RightMenuDemo.getSharedInstance());
		
		
		JFrame f = new JFrame();

		f.getContentPane().add(tf, BorderLayout.CENTER);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(600, 300);
		// f.pack();
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}
}
